package net;

public class Util {

    public static byte[] intToByteArray(int a) {
        byte[] res = new byte[4];
        intToByteArray(a, res, 0);
        return res;
    }

    public static int byteArrayToInt(byte[] source, int begin) {
        int res = 0;
        for (int i = 3; i >= 0; i--) {
            int add = source[begin + i] & 255;
            res = (res << 8) | add;
        }
        return res;
    }

    public static int byteArrayToInt(byte[] source) {
        return byteArrayToInt(source, 0);
    }

    public static void intToByteArray(int a, byte[] res, int begin) {
        for (int i = 0; i < 4; i++) {
            res[begin + i] = (byte) (a & 255);
            a >>>= 8;
        }
    }

    public static void intArrayToByteArray(int[] a, byte[] res, int begin) {
        for (int i = 0; i < a.length; i++) {
            intToByteArray(a[i], res, begin + i * 4);
        }
    }

    public static int[] byteArrayToIntArray(byte[] source, int begin, int length) {
        int[] res = new int[length];
        for (int i = 0; i < length; i++) {
            res[i] = byteArrayToInt(source, begin + i * 4);
        }
        return res;
    }
}
